package com.laikan.reader.core.view;

import android.content.Context;
import android.graphics.Color;
import android.os.Handler;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.ScrollView;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;

/**
 * @author wx
 * @date 2016/3/14:13:41
 * @description 自动滚动布局
 */
public class AutoScrollView extends ScrollView {

    LinearLayout content; //子布局
    LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
    /**
     * @param context
     * @param attrs
     */
    private int offser = 0;//偏移量
    private List<String> list;
    private Context context;
    private int textsize = 20;//文字大小
    private int position = 0;//记录循环数组下标
    private int dex = 0;//记录所有view的下标
    private boolean isInitiate;//是否启动动画
    private int measureHeight;
    private float density;//屏幕密度
    private Timer autoUpdate;//定时器
    private int marginLeft = 15;
    private Handler handler = new Handler() {
        public void handleMessage(android.os.Message msg) {
            smoothScrollBy(0, 1);

            if (offser % textsize * density == 0 && offser != 0) {

                if (position < list.size()) {
                }else {
                    position = 0;
                }
                addViewChild(position);
                //              content.removeView(content.getChildAt(dex));
                dex++;
                position++;

            }

        }

    };

    public AutoScrollView(Context context, AttributeSet attrs) {
        super(context, attrs);
        // TODO Auto-generated constructor stub
        this.context = context;
        list = new ArrayList<String>();
        density = context.getResources().getDisplayMetrics().density;
        params.leftMargin = (int)(marginLeft * density);
    }

    @Override
    public boolean onTouchEvent(MotionEvent ev) {
        // TODO Auto-generated method stub
        return false;
    }

    /**
     * 添加view并启动动画
     */
    private void setView() {
        if (getChildCount() > 0) {
            content = (LinearLayout)getChildAt(0);
            addView();
            measure(this);
            if (measureHeight > 400) {
                startAnim();
            }else {
            }
        }
    }

    private void addView() {
        for (int i = 0; i < list.size(); i++) {
            addViewChild(i);
        }
    }

    public void addViewChild(int i) {
        TextView tx;
        tx = new TextView(context);
        tx.setText(list.get(i));
        tx.getPaint().setTextSize(density * 13);
        tx.setLayoutParams(params);
        tx.setTextColor(Color.parseColor("#999999"));
        content.addView(tx);
    }

    private void startAnim() {
        autoUpdate = new Timer();
        autoUpdate.schedule(new TimerTask() {
            @Override
            public void run() {
                offser += 1;
                handler.sendEmptyMessage(1);
            }
        }, 500, 30);
    }

    protected void measure(View v) {
        int w = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED);
        int h = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED);
        v.measure(w, h);
        measureHeight = v.getMeasuredHeight();
        int width = v.getMeasuredWidth();
    }

    /**
     * 开始动画
     */
    public void setAnimBegin() {
        if (list != null && list.size() > 0) {
            startAnim();
        }
    }

    /**
     * 结束动画
     */
    public void setAnimStop() {
        if (autoUpdate != null) {
            autoUpdate.cancel();
            autoUpdate = null;
        }
    }

    public void setData(List<String> ls) {
        this.list = ls;
        if (list != null && list.size() > 0) {
            setView();
        }
    }
}